<?php

namespace app\common\libs\helper;

/**
 * 树状结构工具助手类
 *
 * @Author tanruizheng
 * @DateTime 2021-07-30
 */
class TreeHelper
{
    
    /**
     *  根据给定的散列数组结构，整理成树状结构
     *
     * @Author tanruizheng
     * @DateTime 2021-07-30
     * @param array $array
     * @param [type] $pid
     * @param array $config
     * @param integer $level
     * @return void
     */
    static function arrayToTree(array $array, $pid, $config = [], $level = 0)
    {
        $curConfig = [
            'idKey'       => isset($config['idKey']) ? $config['idKey'] : 'id',// 节点的ID字段名
            'parentKey'   => isset($config['parentKey']) ? $config['parentKey'] : 'pid', // 父节点的ID字段名
            'childrenKey' => isset($config['childrenKey']) ? $config['childrenKey'] : 'children', // 子列表的key名
            'maxLevel'    => isset($config['maxLevel']) ? $config['maxLevel'] : 0,// 最大层级，0为不限制。父节点算一层
            'levelKey'    => isset($config['levelKey']) ? $config['levelKey'] : 'level',// 层级的key名，按从1开始
        ];
        $nodeList = [];
        foreach ($array as $index => $item) {
            if ($item[$curConfig['parentKey']] == $pid) {
                // 寻找下一级
                if ($curConfig['maxLevel'] === 0 || $level + 1 <= $curConfig['maxLevel']) {
                    if (!empty($curConfig['levelKey'])) {
                        $item[$curConfig['levelKey']] = $level;
                    }
                    $item[$curConfig['childrenKey']] = self::arrayToTree($array, $item[$curConfig['idKey']], $config, $level + 1);
                }
                $nodeList[] = $item;
            }
        }
        return $nodeList;
    }

    /**
     *  根据给定的散列数组结构，整理成树状结构
     *
     * @Author tanruizheng
     * @DateTime 2021-07-30
     * @param array $array
     * @param [type] $pid
     * @param array $config
     * @param integer $level
     * @return void
     */
    static function arrayToTreeList(array $array, $pid, $config = [], $level = 0)
    {
        $curConfig = [
            'idKey'       => isset($config['idKey']) ? $config['idKey'] : 'id',// 节点的ID字段名
            'parentKey'   => isset($config['parentKey']) ? $config['parentKey'] : 'pid', // 父节点的ID字段名
            'maxLevel'    => isset($config['maxLevel']) ? $config['maxLevel'] : 0,// 最大层级，0为不限制。父节点算一层
            'levelKey'    => isset($config['levelKey']) ? $config['levelKey'] : 'level',// 层级的key名，按从1开始
        ];
        static $nodeList = [];
        foreach ($array as $index => $item) {
            if ($item[$curConfig['parentKey']] == $pid) {
                // 寻找下一级
                if ($curConfig['maxLevel'] === 0 || $level + 1 <= $curConfig['maxLevel']) {
                    if (!empty($curConfig['levelKey'])) {
                        $item[$curConfig['levelKey']] = $level;
                    }
                    $nodeList[] = $item;
                    unset($array[$index]);
                    self::arrayToTreeList($array, $item[$curConfig['idKey']], $config, $level + 1);
                }
              
            }
        }
        return $nodeList;
    }

    /**
     * 获取所有子级
     *
     * @Author tanruizheng
     * @DateTime 2021-07-12
     * @param array $array
     * @param [type] $pid
     * @return array
     */
    static function getSonNodeFromArray(array $array, $pid, $config = []){
        $curConfig = [
            'parentKey'   => isset($config['parentKey']) ? $config['parentKey'] : 'pid', // 父节点的ID字段名
        ];
        $nextList = [$pid];
        $result = [];
        while($nextList){
            $nextRes = [];
            foreach ($array as $key => $value) {
                if(in_array($value[$curConfig['parentKey']],$nextList)){
                    $nextRes[] = $value;
                    $result[] = $value;
                }else{
                    $nextArr[] = $value;
                }
            }
            $nextList = $nextRes;
            $array = $nextArr;
        }
        return $result ?: [];
    }
}